import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/proportal"
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login.vue")
  },
  {
    path: "/systemtools",
    name: "systemtools",
    redirect: '/systemtools/main',
    component: () => import("@/views/systemtools/systemtools.vue"),
    children: [
      {
        path: "main",
        name: "main",
        component: () => import("@/views/systemtools/main.vue"),
      },
      {
        path: "portaladmin",
        redirect: 'portalAdmin/WorkflowType',
        component: () => import("@/views/tempVue.vue"),
        children: [
          {
            path: 'WorkflowType',
            name: 'WorkflowType',
            component: () => import("@/views/systemtools/portalAdmin/WorkflowType.vue")
          },
          {
            path: 'NewsManage',
            name: 'NewsManage',
            component: () => import("@/views/systemtools/portalAdmin/NewsManage.vue")
          },
          {
            path: 'PublicModelAdd',
            name: 'PublicModelAdd',
            component: () => import("@/views/systemtools/portalAdmin/PublicModelAdd.vue")
          },
          {
            path: 'NoticesManage',
            name: 'NoticesManage',
            component: () => import("@/views/systemtools/portalAdmin/NoticesManage.vue"),
          },
          {
            path: 'RulesManage',
            name: 'RulesManage',
            component: () => import("@/views/systemtools/portalAdmin/RulesManage.vue")
          },
          {
            path: 'MenuManage',
            name: 'MenuManage',
            component: () => import("@/views/systemtools/portalAdmin/MenuManage.vue")
          },
          {
            path: 'ApplySort',
            name: 'ApplySort',
            component: () => import("@/views/systemtools/portalAdmin/ApplySort.vue")
          }
        ]
      },
      {
        path: 'processManagePlatform',
        component: () => import("@/views/tempVue.vue"),
        redirect: 'processManagePlatform/rule',
        children: [
          {
            path: 'rule',
            name: 'rule',
            component: () => import('@/views/systemtools/processManagePlatform/rule.vue'),
          },
          {
            path: 'rule/edit',
            name: 'ruleEdit',
            component: () => import("@/views/systemtools/processManagePlatform/ruleEdit.vue")
          },
          {
            path: 'ProcessIntervention',
            name: 'ProcessIntervention',
            component: () => import("@/views/systemtools/processManagePlatform/processManage/ProcessIntervention.vue")
          },
          {
            path: 'InterventionLog',
            name: 'InterventionLog',
            component: () => import("@/views/systemtools/processManagePlatform/processManage/InterventionLog.vue")
          },
          {
            path: 'ProcessSystem',
            name: 'ProcessSystem',
            component: () => import("@/views/systemtools/processManagePlatform/ProcessSystem.vue")
          }
        ]
      },
      {
        path: 'systemtools',
        component: () => import("@/views/tempVue.vue"),
        redirect: 'systemManage/OrganizationalStructure',
        children: [
           {
              path: 'OrganizationalStructure',
              name: 'OrganizationalStructure',
              component: () => import("@/views/systemtools/systemManage/OrganizationalStructure.vue")
           },
           {
             path: 'RoleManagement',
             name: 'RoleManagement',
             component: () => import("@/views/systemtools/systemManage/RoleManagement.vue")
           },
           {
             path: 'FunctionalAuthority',
             name: 'FunctionalAuthority',
             component: () => import("@/views/systemtools/systemManage/FunctionalAuthority.vue") 
           },
           {
             path: 'SecurityManagement',
             name: 'SecurityManagement',
             component: () => import("@/views/systemtools/systemManage/SecurityManagement.vue")
           },
           {
             path: 'BusinessManagement',
             name: 'BusinessManagement',
             component: () => import("@/views/systemtools/systemManage/BusinessManagement.vue")
           },
           {
             path: 'IconManagement',
             name: 'IconManagement',
             component: () => import("@/views/systemtools/systemManage/IconManagement.vue")
           }
        ]
      }
    ]
  },
  {
    path: '/proportal',
    name: "proportal",
    redirect: '/proportal/index',
    component: () => import("@/views/proportalTempVue.vue"),
    children: [
      {
        path: "index",
        name: "index",
        component: () => import("@/views/proportal/proportal.vue"),
      },
      {
        path: "processCenter",
        name: "processCenter",
        component: () => import("@/views/proportal/processCenter.vue"),
      },
      {
        path: "personalCenter",
        name: 'personalCenter',
        component: () => import("@/views/proportal/personalCenter.vue")
      },
      {
        path: "new",
        name: "new",
        component: () => import("@/views/proportal/news/new.vue"),
      },
      {
        path: "examine",
        name: "examine",
        component: () => import("@/views/proportal/news/examine.vue"),
      },
      {
        path: "kpim",
        name: "kpim",
        component: () => import("@/views/proportal/news/kpim.vue"),
      },
      {
        path: "handover",
        name: "handover",
        component: () => import("@/views/proportal/news/handOver.vue"),
      },
      {
        path: "formalapply",
        name: "formalapply",
        component: () => import("@/views/proportal/news/formalApply.vue"),
      },
      {
        path: "runapply",
        name: "runapply",
        component: () => import("@/views/proportal/news/runApply.vue"),
      },
      {
        path: "seal",
        name: "seal",
        component: () => import("@/views/proportal/news/seal.vue"),
      },
      {
        path: "material",
        name: "material",
        component: () => import("@/views/proportal/news/material.vue"),
      },
      {
        path: "remote",
        name: "remote",
        component: () => import("@/views/proportal/news/remote.vue"),
      },
      {
        path: "holiday",
        name: "holiday",
        component: () => import("@/views/proportal/news/holiday.vue"),
      },
      {
        path: "viewDetail",
        name: "viewDetail",
        component: () => import("@/components/proportal/portalIndex/viewDetail")
      },
      {
        path: 'appCenter',
        name: 'appCenter',
        component: () => import("@/views/proportal/appCenter.vue")
      }
    ]
  },
  {
    path: "/403",
    name: '403',
    component: () => import("@/components/public/403.vue")
  },
  {
    path: '/404',
    name: '404',
    component: () => import("@/components/public/404.vue")
  },
  {
    path: "*",
    component: () => import("@/components/public/404.vue")
  }
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  let toRes = to.path.split("/")
  let toResLast = toRes[toRes.length - 1]
  let whitePath = ["main", "403", "404", "login", "PublicModelAdd"]
  let allPath = JSON.parse(localStorage.getItem("allMenu")) || [];
  if(whitePath.includes(toResLast)) {
      next()
  } else {
    if(toRes[1] === 'systemtools') {
      let menus = JSON.parse(localStorage.getItem("menu") || "");
      if(menus.includes(toResLast)) {
        next()
      } else {
        let ifNext = false
        for (let index = 2; index < toRes.length; index++) {
          const element = toRes[index];
          if(!allPath.includes(element)) {
            if(element === "edit" && toRes[index - 1] && toRes[index - 1] === "rule") {
              next()
            } else {
              next({
                path: '/404',
                replace: true
              })
            }
            ifNext = true
          }
        }
        if(!ifNext) {
          if(!menus.includes(toResLast)) {
            next({
              path: '/403',
              replace: true
            })
          } else {
            next()
          }
        }
      }
    } else if(toRes[1] === "proportal") {
      next()
    } else {
      next({
        path: '/404',
        replace: true
      })
    }
  }
})

export default router;
